﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Dala.Samples.Multiple_Image_With_Colorbox
{
    public partial class multiple_image_with_colorbox : System.Web.UI.Page
    {
        public class ProductCategory
        {
            public int CategoryId { get; set; }
            public int ParentCategoryId { get; set; }
            public string CategoryName { get; set; }
            public string ImageUrl { get; set; }
        }
        ProductCategory[] products = new ProductCategory[]
                {  
                    new ProductCategory { CategoryId = 2, ParentCategoryId = 1, CategoryName = "A", ImageUrl="images/1.jpg"},  
                    new ProductCategory { CategoryId = 3, ParentCategoryId = 1, CategoryName = "B", ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 4, ParentCategoryId = 1, CategoryName = "C", ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 5, ParentCategoryId = 1, CategoryName = "D", ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 6, ParentCategoryId = 1, CategoryName = "E", ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 7, ParentCategoryId = 2, CategoryName = "A1", ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 8, ParentCategoryId = 2, CategoryName = "A2", ImageUrl="images/2.jpg"}, 
                    new ProductCategory { CategoryId = 9, ParentCategoryId = 2, CategoryName = "A3", ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 10, ParentCategoryId = 3, CategoryName = "B1",ImageUrl="images/2.jpg"}, 
                    new ProductCategory { CategoryId = 11, ParentCategoryId = 3, CategoryName = "B2",ImageUrl="images/3.jpg"}, 
                    new ProductCategory { CategoryId = 12, ParentCategoryId = 3, CategoryName = "B3",ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 13, ParentCategoryId = 4, CategoryName = "C1",ImageUrl="images/4.jpg"}, 
                    new ProductCategory { CategoryId = 14, ParentCategoryId = 4, CategoryName = "C2",ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 15, ParentCategoryId = 4, CategoryName = "C3",ImageUrl="images/2.jpg"}, 
                    new ProductCategory { CategoryId = 16, ParentCategoryId = 5, CategoryName = "D1",ImageUrl="images/1.jpg"}, 
                    new ProductCategory { CategoryId = 17, ParentCategoryId = 5, CategoryName = "D2",ImageUrl="images/2.jpg"}, 
                    new ProductCategory { CategoryId = 18, ParentCategoryId = 5, CategoryName = "D3",ImageUrl="images/3.jpg"}, 
                    new ProductCategory { CategoryId = 19, ParentCategoryId = 6, CategoryName = "E1",ImageUrl="images/4.jpg"}, 
                    new ProductCategory { CategoryId = 20, ParentCategoryId = 6, CategoryName = "E2",ImageUrl="images/2.jpg"},
                    new ProductCategory { CategoryId = 21, ParentCategoryId = 6, CategoryName = "E3",ImageUrl="images/1.jpg"}
                };
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Count"] = 1;
            if (!IsPostBack)
            {
                bindData();
            }
        }
        protected void rptImages_ItemDataBound(Object sender, RepeaterItemEventArgs e)
        {
            ProductCategory productCategory = e.Item.DataItem as ProductCategory;
            if (productCategory != null)
            {

                var bindViewMoreImageDiv = (HtmlGenericControl)e.Item.FindControl("ViewMoreImages");
                var additionalImages = from c in products
                                       where c.ParentCategoryId == productCategory.CategoryId
                                       select c;
                if (additionalImages.Any())
                {
                    string bindDiv = "";
                    int initial = 1;
                    int count = Convert.ToInt32(Session["Count"]);
                    foreach (var image in additionalImages)
                    {
                        if (initial == 1)
                        {
                            bindDiv += "<p><a href='" + image.ImageUrl + "'" + " class='group" + count + "'>ClickHere</a></p>";
                        }
                        else
                        {
                            bindDiv += "<p><a href='" + image.ImageUrl + "'" + " class='group" + Session["Count"] + "'></a></p>";
                        }
                        initial++;
                    }
                    bindViewMoreImageDiv.InnerHtml = bindDiv;
                }
                int assignValueToSession = Convert.ToInt32(Session["Count"].ToString());
                Session["Count"] = assignValueToSession + 1;
            }
        }
        private void bindData()
        {
            var productCategories = from c in products
                                    where c.ParentCategoryId == 1
                                    select c;
            rptImages.DataSource = productCategories;
            rptImages.DataBind();
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            int count = Convert.ToInt32(Session["Count"].ToString());

            string myScript = "";
            for (var i = 1; i < count; i++)
            {
                myScript += "$('.group" + i + "').colorbox({ rel: 'group" + i + "' });";
            }

            Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript.ToString(), true);
        }
    }
}